﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASoft.Web.Base
{
    public class LoginService
    {
        public void Login(string code, string password)
        {
            if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(password))
            {
                throw new Exception("请输入用户名或密码.");
            }
            AUserRepository userRepository = new AUserRepository();
            var auser = userRepository.GetEntities(new Dictionary<string, string>() { { "Code", code } }).FirstOrDefault();
            if (string.Equals(code, "admin", StringComparison.CurrentCultureIgnoreCase))
            {
                CheckAdmin();
                if (auser == null)
                {
                    auser = userRepository.GetEntities(new Dictionary<string, string>() { { "Code", code } }).FirstOrDefault();
                }
            }
            else if (auser == null)
            {
                throw new Exception("用户名或密码错误.");
            }
            if (auser.CPassword != password)
            {
                throw new Exception("用户名或密码错误.");
            }
            CurrentContext.AUser = auser;
            CurrentContext.IsLogin = true;

        }

        void CheckAdmin()
        {
            AUserRepository userRepository = new AUserRepository();
            var auser = userRepository.GetEntities(new Dictionary<string, string>() { { "Code", "admin" } }).FirstOrDefault();
            if (auser == null)
            {
                auser = new AUser();
                auser.Code = "admin";
                auser.CPassword = "123456";
                auser.Enable = 1;
                auser.CName = "管理员";
                userRepository.Insert(auser);
                auser = userRepository.GetEntities(new Dictionary<string, string>() { { "Code", "admin" } }).FirstOrDefault();
            }

            CustomRepository customRepository = new CustomRepository();
            var custom = customRepository.GetEntities(new Dictionary<string, string>() { { "Code", "admin" } }).FirstOrDefault();
            if (custom == null)
            {
                custom = new Custom();
                custom.Code = "admin";
                custom.CName = "系统";
                custom.Enable = 1;
                customRepository.Insert(custom);
                custom = customRepository.GetEntities(new Dictionary<string, string>() { { "Code", "admin" } }).FirstOrDefault();
            }

            CustomAUserRepository customAUserRepository = new CustomAUserRepository();
            var ca = customAUserRepository.GetEntities(
                new Dictionary<string, string>() { 
                    { "AUserId", auser.AUserId.ToString() },
                { "CustomId", custom.CustomId.ToString() }}).FirstOrDefault();
            if (ca == null)
            {
                ca = new CustomAUser();
                ca.AUserId = auser.AUserId;
                ca.CustomId = custom.CustomId;
                customAUserRepository.Insert(ca);
            }
        }

        public void Logout()
        {
            CurrentContext.AUser = null;
            CurrentContext.IsLogin = false;
        }
    }
}
